package com.shuoyue.ycgk.ui.papergroups;

import com.shuoyue.ycgk.base.BaseModel;
import com.shuoyue.ycgk.base.BasePresenter;
import com.shuoyue.ycgk.base.BaseResult;
import com.shuoyue.ycgk.base.BaseView;
import com.shuoyue.ycgk.base.ListWithPage;
import com.shuoyue.ycgk.entity.Paper;
import com.shuoyue.ycgk.entity.PaperSet;
import com.shuoyue.ycgk.net.RetrofitClient;
import com.shuoyue.ycgk.net.base.ApiSubscriber;
import com.shuoyue.ycgk.net.gsondefaultadapter.Optional;

import java.util.List;

import io.reactivex.Observable;
import retrofit2.http.Field;

public interface PaperContract {
    interface View extends BaseView {
        void setPaperSet(List<PaperSet> paperSets);

        void setPaperSet(ListWithPage<PaperSet> paperSets);

        void setPaperSetInfo(PaperSet paperInfo);


        void setPaperList(ListWithPage<Paper> listWithPage);
    }

    class Presenter extends BasePresenter<View> {
        Model model = new Model();

        /**
         * 获取题库
         *
         * @param type     1真题,
         * @param isMine   是否已购买
         * @param moduleId
         */
        public void getPaperSet(Integer moduleId, String title, Integer type, boolean isMine) {
            apply(model.getPaperSet(moduleId, title, type))
                    .subscribe(new ApiSubscriber<Optional<List<PaperSet>>>(mView, this) {
                        @Override
                        public void onNext(Optional<List<PaperSet>> listOptional) {
                            super.onNext(listOptional);
                            mView.setPaperSet(listOptional.getIncludeNull());
                        }
                    });
        }


        public void getPaperInfo(int topicId) {
            apply(model.getPaperInfo(topicId))
                    .subscribe(new ApiSubscriber<Optional<PaperSet>>(mView, this) {
                        @Override
                        public void onNext(Optional<PaperSet> listOptional) {
                            super.onNext(listOptional);
                            mView.setPaperSetInfo(listOptional.getIncludeNull());
                        }
                    });
        }

        public void getPaperList(int topicId, String title, int page) {
            apply(model.getTopicPaper(topicId, title, page))
                    .subscribe(new ApiSubscriber<Optional<ListWithPage<Paper>>>(mView, this) {
                        @Override
                        public void onNext(Optional<ListWithPage<Paper>> listOptional) {
                            super.onNext(listOptional);
                            mView.setPaperList(listOptional.getIncludeNull());
                        }
                    });
        }
    }

    class Model extends BaseModel {


        /**
         * 题库包列表
         *
         * @param moduleId
         * @return
         */
        Observable<BaseResult<List<PaperSet>>> getPaperSet(Integer moduleId, String title, Integer type) {
            if (type != null && type == 2) {
                return RetrofitClient.getInstance().getApi().getPaperSetHotList(moduleId, type);
            } else

                return RetrofitClient.getInstance().getApi().getPaperSetList(null, moduleId, title, type);
        }

        /**
         * 题库包详情（试卷列表）
         *
         * @param topicId
         * @return
         */
        Observable<BaseResult<PaperSet>> getPaperInfo(int topicId) {
            return RetrofitClient.getInstance().getApi().getPaperSetInfo(topicId);
        }

        /**
         * 题库包详情（试卷列表）
         *
         * @param topicId
         * @return
         */
        Observable<BaseResult<ListWithPage<Paper>>> getTopicPaper(int topicId, String title, int page) {
            return RetrofitClient.getInstance().getApi().getTopicPaper(topicId, title, page);
        }
    }
}
